<?php
/**
 * 
 * 计划任务类
 * @author hjc19850430@gmail.com
 */
namespace Task\App;

use Task\Core\Base;
use Task\Core\CreateGlobalPk;

class Crontab extends Base
{
    public $mysql;
    public $accountId;

    public function __construct()
    {
        parent::__construct();
        $this->mysql = $this->selectDB('local_mysql');
        $this->accountId = config('jiliang_account_id');
    }

    /**
     * 测试生成全局主键值
     */
    public function test()
    {
        $GlobalPk = new CreateGlobalPk();
        $id = $GlobalPk->getPk('doorctrl_stat');

        cdump($id);
    }

    //更新统计表的全局主键预设id
    public function updateStatSeq()
    {
        $staticsId = 188524;

        $sql = "select count(*) as cnt from gc_doorctrl_stat_2017_01";
        $data = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);
        $pageSize = 3000;
        $pageNum = ceil($data['cnt'] / $pageSize);

        for($i=0; $i < $pageNum; $i++)
        {
            $offset = $pageSize * $i;
            $sql = "select statics_id from gc_doorctrl_stat_2017_01 order by statics_id asc limit $offset,$pageSize";
            $data = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
            if (!$data) {
                continue;
            }

            foreach($data as $d) {
                $sql = "update gc_doorctrl_stat_2017_01 set seq_id={$staticsId} where statics_id={$d['statics_id']}";
                $this->mysql->exec($sql);

                $staticsId++;
            }

            sleep(1);

            echo "success {$i}\n";
        }
    }

    //更新确认日志表中的全局主键id
    public function updateEnterLogStatSeq()
    {
        $sql = "select count(*) as cnt from gc_doorctrl_enter_log_2017_01";
        $data = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);
        $pageSize = 3000;
        $pageNum = ceil($data['cnt'] / $pageSize);

        for($i=0; $i < $pageNum; $i++)
        {
            $offset = $pageSize * $i;
            $sql = "select statics_id from gc_doorctrl_enter_log_2017_01 order by statics_id asc limit $offset,$pageSize";
            $data = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
            if (!$data) {
                continue;
            }

            foreach($data as $d) {
                $selSql = "select seq_id from gc_doorctrl_stat_2017_01 where statics_id={$d['statics_id']}";
                $tmpData = $this->mysql->query($selSql)->fetch(\PDO::FETCH_ASSOC);

                $sql = "update gc_doorctrl_enter_log_2017_01 set stat_seq_id={$tmpData['seq_id']} where statics_id={$d['statics_id']}";
                $this->mysql->exec($sql);
            }
            unset($data);

            sleep(1);

            echo "success {$i}\n";
        }
    }


    /**
     * 更新确认统计表中的staticis_id
     */
    public function updateStaticsId()
    {
        $sql = "update gc_doorctrl_stat_2017_01 set statics_id=seq_id";
        $res = $this->mysql->exec($sql);
        if ($res) {
            echo "update statics success!\n";
        }
    }

    /**
     * 更新确认日志表中的staticis_id
     */
    public function updateEnterLogStaticsId()
    {
        $sql = "update gc_doorctrl_enter_log_2017_01 set statics_id=stat_seq_id";
        $res = $this->mysql->exec($sql);
        if ($res) {
            echo "update enter log success!\n";
        }
    }


    /***
     * 将gc_user_copy表中的数据拷贝到gc_user表中去
     * @param $params
     */
    public function copyUser()
    {
        $sql = "select count(*) as totalNum from gc_user_copy where account_id={$this->accountId}";
        $totalNumInfo = $this->mysql->query($sql)->fetch(\PDO::FETCH_ASSOC);
        if (!$totalNumInfo['totalNum']) {
            wLog('user data is null!','copyUser');
            exit;
        }

        $pageSize = 2000;
        $pageNum = ceil($totalNumInfo['totalNum'] / $pageSize);
        for ($i=0; $i < $pageNum; $i++) {
            $offset = $i * $pageSize;
            $sql = "select * from gc_user_copy where account_id=33 limit {$offset},$pageSize";
            $data = $this->mysql->query($sql)->fetchAll(\PDO::FETCH_ASSOC);
            if (!$data) {
                wLog('user_copy data is null!','copyUser');
                exit;
            }

            //插入到user表中去
            $insertData = array();
            foreach($data as $d) {
                unset($d['user_id']);
                $insertData[] = $d;
            }

            $res = $this->mysql->insert('user',$insertData);
            if (!$res) {
                wLog('add user failed!','copyUser');
            } else {
                echo "add user success:$i\n";
            }

            sleep(1);
        }
    }



}